package examples

import (
	"fmt"
	"gitee.com/Cauchy_AQ/gokv"
	"gitee.com/Cauchy_AQ/gokv/utils"
)

func ExamplesIterator() {
	opts := gokv.DefaultOptions
	db, err := gokv.Open(&opts)
	defer db.Close()
	if err != nil {
		panic(err)
	}

	for i := 0; i < 5; i++ {
		_ = db.Put(utils.GetTestKey(i), utils.GetTestValue(i))
		_ = db.Put([]byte("iter"+string(utils.GetTestKey(i))), utils.GetTestValue(i))
	}

	iteratorOpts := gokv.DefaultIteratorOptions
	iteratorOpts.Prefix = []byte("iter")
	iteratorOpts.Reverse = true
	iter := db.NewIterator(&iteratorOpts)
	defer iter.Close()

	iter.Rewind()
	for iter.Valid() {
		val, _ := iter.Value()
		fmt.Println(string(iter.Key()), string(val))
		iter.Next()
	}

	for iter.Rewind(); iter.Valid(); iter.Next() {
		val, _ := iter.Value()
		fmt.Println(string(iter.Key()), string(val))
	}
}
